package com.zxs.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zxs.common.entity.PageResult;
import com.zxs.common.entity.Result;
import com.zxs.domain.entity.dto.SubjectInfoDTO;
import com.zxs.domain.service.SubjectInfoDomainService;
import com.zxs.infra.basic.service.SubjectInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 题目
 * @author zxs
 * @date 2025/11/12 下午9:12
 */
@RestController
@Slf4j
public class SubjectInfoController {

    @Autowired
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> insertCategory(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        log.info("新增题目传递的参数-----{}", JSON.toJSONString(subjectInfoDTO));
        Result<Boolean> result = new Result<>();
        try {
            //参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "题目名称不能为空！");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空！");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空！");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空！");
            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()),"分类id不能为空！");
            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()),"标签id不能为空！");

            Boolean aBoolean = subjectInfoDomainService.insertSubInfo(subjectInfoDTO);
            if (aBoolean) {
                result = Result.success("新增题目成功！", aBoolean);
            }
        } catch (Exception e) {
            log.error("新增题目，报错{}", e.getMessage());
            result = Result.failed("服务器异常，请联系管理员！");
            return result;
        }
        return result;
    }

    /**
     * 查询题目列表
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        log.info("查询题目列表传递的参数-----{}", JSON.toJSONString(subjectInfoDTO));
        Result<PageResult<SubjectInfoDTO>> result = new Result<>();
        try {
            //参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类ID不能为空！");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签ID不能为空！");

            PageResult<SubjectInfoDTO> resultData = subjectInfoDomainService.getSubjectPage(subjectInfoDTO);

            result = Result.success("查询题目列表成功！", resultData);
        } catch (Exception e) {
            log.error("新增题目，报错{}", e.getMessage());
            result = Result.failed("服务器异常，请联系管理员！");
        }
        return result;
    }

    /**
     * 查询题目详情
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        log.info("查询题目列表传递的参数-----{}", JSON.toJSONString(subjectInfoDTO));
        Result<SubjectInfoDTO> result = new Result<>();
        try {
            //参数校验
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类ID不能为空！");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签ID不能为空！");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectId(), "题目ID不能为空！");

            result = subjectInfoDomainService.querySubjectInfo(subjectInfoDTO);

        } catch (Exception e) {
            log.error("新增题目，报错{}", e.getMessage());
            result = Result.failed("服务器异常，请联系管理员！");
        }
        return result;
    }

}